﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;

namespace Edu.Web.Data.DAO.Impl
{
    public partial class TimeTableDAO
    {
        public IQueryable<TimeTableDV> GetTimeTableList()
        {
            return from timeTable in Db.Set<TimeTableEntity>()
                   join clazz in Db.Set<ClazzEntity>() on timeTable.ClazzId equals clazz.Id
                   orderby timeTable.CreateTime descending
                   select new TimeTableDV
                   {
                       Id = timeTable.Id,
                       ClazzId = clazz.Id,
                       ClazzName = clazz.ClazzName,
                       CreateTime = timeTable.CreateTime,
                       Remark = timeTable.Remark,
                       TableName = timeTable.TableName
                   };
        }

        public IQueryable<TimeTableCourceDV> GetTimeTableCourceList(long timeTableId)
        {
            return from tp in Db.Set<TimeTableProjectEntity>()
                   join cu in Db.Set<CourceEntity>() on tp.CourceId equals cu.Id
                   join teacher in Db.Set<UserEntity>() on tp.UserId equals teacher.Id
                   where tp.TimeTableId == timeTableId
                   orderby tp.WeekDay, tp.StartTime, tp.StopTime, cu.CourceName
                   select new TimeTableCourceDV
                   {
                       Address = tp.Address,
                       WeekDay = tp.WeekDay,
                       CourceId = tp.CourceId,
                       Id = tp.Id,
                       TeacherName = teacher.RealName,
                       CourceName = cu.CourceName,
                       Remark = tp.Remark,
                       UserId = tp.UserId,
                       TimeTableId = tp.TimeTableId,
                       StartTime = tp.StartTime.ToString(),
                       StopTime = tp.StopTime.ToString()
                   };
        }

        public IQueryable<CourceProjectDV> GetCourceProjectList()
        {
            return from tp in Db.Set<TimeTableProjectEntity>()
                   join cource in Db.Set<CourceEntity>() on tp.CourceId equals cource.Id
                   join tt in Db.Set<TimeTableEntity>() on tp.TimeTableId equals tt.Id
                   select new CourceProjectDV
                   {
                       CourceId = tp.CourceId,
                       CourceName = cource.CourceName,
                       UserId = tp.UserId,
                       ClazzId = tt.ClazzId
                   };
        }

        public IQueryable<CourceStudentDV> GetCourceStudentList()
        {
            return from tp in Db.Set<TimeTableProjectEntity>()
                   join cource in Db.Set<CourceEntity>() on tp.CourceId equals cource.Id
                   join tt in Db.Set<TimeTableEntity>() on tp.TimeTableId equals tt.Id
                   join clazz in Db.Set<ClazzEntity>() on tt.ClazzId equals clazz.Id
                   join stuclazz in Db.Set<ClazzStuEntity>() on clazz.Id equals stuclazz.ClazzId
                   join stu in Db.Set<UserEntity>() on stuclazz.StuUserId equals stu.Id
                   join stuD in Db.Set<StudentEntity>() on stu.Id equals stuD.UserId
                   select new CourceStudentDV
                   {
                       ClazzId = clazz.Id,
                       ClazzName = clazz.ClazzName,
                       CourceId = cource.Id,
                       CourceName = cource.CourceName,
                       RealName = stu.RealName,
                       StuNo = stuD.StuNo,
                       StuUserId = stu.Id,
                       TeacherUserId = tp.UserId
                   };
        }
    }
}